//
//  InviteFriendsViewController.swift
//  CoolTennisBall
//
//  Created by CA on 16/4/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

import UIKit
import MBProgressHUD

final class InviteFriendsViewController: UIViewController, UIGestureRecognizerDelegate {
    @IBOutlet weak var shareView: UIView!
    
    // MARK: Constants
    
    fileprivate struct Constants {
        static let WeiXinShareButtonTag = 100
        static let QQZoneShareButtonTag = 101
        static let QQShareButtonTag = 102
        static let PenYouQuanButtonTag = 103
    }
    
    deinit {
        print("dealloc: \(self)")
    }
    
    // MARK: View LifeCycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addTapGestureRecognzier()
    }
    
    // MARK: IBAction
    
    @IBAction func shareButtonDidTouch(_ sender: UIButton) {
        
        switch sender.tag {
        case Constants.WeiXinShareButtonTag, Constants.PenYouQuanButtonTag:
            if !WXApi.isWXAppInstalled() {
                MBProgressHUD.showTextHUD(withMessage: "用户没有安装\"微信\"应用", in: view)
                return
            }
        case Constants.QQShareButtonTag, Constants.QQZoneShareButtonTag:
            if !QQApiInterface.isQQInstalled() {
                MBProgressHUD.showTextHUD(withMessage: "用户没有安装\"QQ\"应用", in: view)
                return
            }
        default: break
        }
        
        self.dismiss(animated: false, completion: nil)
        switch sender.tag {
        case Constants.WeiXinShareButtonTag:
            print("微信")
            shareApplication(SSDKPlatformType.subTypeWechatSession)
        case Constants.QQZoneShareButtonTag:
            print("QQ空间")
            shareApplication(SSDKPlatformType.subTypeQZone)
        case Constants.QQShareButtonTag:
            print("QQ分享")
            shareApplication(SSDKPlatformType.subTypeQQFriend)
        case Constants.PenYouQuanButtonTag:
            print("朋友圈")
            shareApplication(SSDKPlatformType.subTypeWechatTimeline)
        default: break
        }
    }
    
    fileprivate func shareApplication(_ type: SSDKPlatformType) {
        ShareMangaer.sharedInstance().share(in: view, withContent: "一个神奇的网球智能小助手，赶紧下载来跟我比一比谁的网球水平更高吧", withTitle: "智能网球", withUrl: "http://a.app.qq.com/o/simple.jsp?pkgname=com.coollang.tennis", share: type) { (success: Bool) in
            
        }
    }
    
    // MARK: Private Method
    
    fileprivate func addTapGestureRecognzier() {
        
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissController))
        tapGestureRecognizer.cancelsTouchesInView = true
        tapGestureRecognizer.delegate = self
        view.addGestureRecognizer(tapGestureRecognizer)
    }
    
    @objc fileprivate func dismissController() {
        self.dismiss(animated: true, completion: nil)
    }
    
    // MARK: UIGestureRecognizerDelegate
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return touch.view != shareView
    }
    
}
